
package com.crm.pojo.common;

import com.crm.pojo.domain.LoginUser;
import com.crm.pojo.domain.User;

public class UserContext {
    private static final ThreadLocal<LoginUser> USER_HOLDER = new ThreadLocal<>();

    public static void setUser(LoginUser loginUser) {
        USER_HOLDER.set(loginUser);
    }

    public static LoginUser getCurrentUser() {
        return USER_HOLDER.get();
    }

    public static Long getCurrentUserId() {
        LoginUser loginUser = getCurrentUser();
        return loginUser != null ? loginUser.getUserId() : null;
    }

    public static String getCurrentUsername() {
        LoginUser loginUser = getCurrentUser();
        return loginUser != null ? loginUser.getUserName() : null;
    }

    public static void clear() {
        USER_HOLDER.remove();
    }
}